package controllers

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"web_app/logic"
	"web_app/models"
	"web_app/tool"
)

//SignupHandler  注册
func SignupHandler(c *gin.Context) {
	var params models.ParamSignUp

	if err := c.ShouldBindJSON(&params); err != nil {
		tool.ReturnVerificationTool(err, c)
		return
	}
	err := logic.SignUp(&params)
	if err != nil {
		tool.Error(c, err.Error())
		return
	}
	tool.Success(c, "注册成功", nil)
	return
}

//LoginHandler  登陆
func LoginHandler(c *gin.Context) {
	var params models.ParamLogin

	if err := c.ShouldBindJSON(&params); err != nil {
		tool.ReturnVerificationTool(err, c)
		return
	}

	err, token := logic.Login(&params)
	if err != nil {
		zap.L().Error("logic.Login() err", zap.String("username", params.UserName), zap.Error(err))
		tool.Error(c, err.Error())
		return
	}

	tool.Success(c, "登陆成功", token)
	return
}

//UserInfoHandler 获取登陆用户信息
func UserInfoHandler(c *gin.Context) {
	userName, _ := c.Get("userName")
	avatar, _ := c.Get("avatar")
	tool.Success(c, "登陆成功", gin.H{
		"user":   "121",
		"roles":  []string{"admin"},
		"name":   userName,
		"avatar": avatar,
	})
	return
}

func UserListHandler(c *gin.Context) {
	var params models.ParamList
	if err := c.ShouldBindQuery(&params); err != nil {
		tool.ReturnVerificationTool(err, c)
		return
	}

	list, err := logic.UserList(&params)
	if err != nil {
		zap.L().Error("logic.UserList err", zap.Error(err))
		tool.Error(c, err.Error())
		return
	}

	tool.Success(c, "", list)
}
